<template>
	<view class="container flex f-column">
		<view class="user-avatar flex">
			<open-data type="userAvatarUrl"></open-data>
		</view>
		<view class="line"></view>
		<text class="font36 gray666" style="margin-top: 100rpx;">申请获取以下权限</text>
		<text class="font30 gray999" style="margin-top: 50rpx;">获得你绑定的手机号</text>
		<button class="yes-login" type="primary" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">允许</button>
		<view @click="back" class="no-login font36 white f-center" style="margin-top: 50rpx;">不允许</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			...mapMutations(['login']),
			getPhoneNumber(e) {
				let _this = this
				_this.$http.ajax({
					url: '/User/getPhoneNumber',
					params: {
						session_key: uni.getStorageSync('sessionKey'),
						encryptedData: e.detail.encryptedData ? e.detail.encryptedData : '',
						iv: e.detail.iv ? e.detail.iv : '',
						openId: uni.getStorageSync('openId')
					},
					isOP: 0
				}).then((res) => {
					if (res.code == 200) {
						_this.$api.msg('授权成功')
						uni.setStorageSync('phone', res.phoneNumber)
						_this.userRegister()
					} else {
						_this.$api.msg(res.msg)
						uni.removeStorageSync('openId')
						uni.removeStorageSync('sessionKey')
						uni.removeStorageSync('info')
						uni.removeStorageSync('merchantId')
						setTimeout(() => {
							uni.navigateBack()
						}, 300)
					}
				})
			},
			getPersonInfo() {
				this.$http.ajax({
					url: '/User/wxSelect',
					params: {
						openId: uni.getStorageSync('openId')
					},
					isOP: 0
				}).then((res) => {
					if (res.code == 200) {
						uni.setStorageSync('userInfo', res.data[0])
						this.login(res.data[0])
					}
				})
			},
			userRegister() {
				let _this = this
				let info = uni.getStorageSync('info')
				_this.$http.ajax({
					url: '/User/add',
					params: {
						'userName': info.nickName,
						'userPhone': uni.getStorageSync('phone'),
						'openId': uni.getStorageSync('openId'),
						'userHeadPortrait': info.avatarUrl
					},
					isOP: 0
				}).then((res) => {
					_this.getPersonInfo()
					setTimeout(() => {
						uni.navigateBack()
					}, 300)
				})
			},
			back() {
				uni.removeStorageSync('openId')
				uni.removeStorageSync('sessionKey')
				uni.removeStorageSync('userInfo')
				setTimeout(() => {
					uni.navigateBack()
				}, 300)
			}
		}
	}
</script>

<style scoped lang="scss">
	.user-avatar {
		width: 240rpx;
		height: 240rpx;
		border-radius: 50%;
		overflow: hidden;
		border: 4rpx solid #FFFFFF;
		box-shadow: 3rpx 3rpx 10rpx rgba(0, 0, 0, 0.2);
		align-self: center;
	}

	.line {
		width: 690rpx;
		height: 4rpx;
		background: #DDDDDD;
		border-radius: 2rpx;
		margin-top: 90rpx;
	}

	.yes-login {
		width: 100%;
		height: 88rpx;
		border-radius: 44rpx;
		margin-top: 120rpx;
	}

	.no-login {
		width: 100%;
		height: 88rpx;
		border-radius: 44rpx;
		background: #DFDFDF;
	}

	.container {
		padding: 60rpx 30rpx 0;
	}
</style>
